﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class OprationWidget : MonoBehaviour
{
    public Vector2 Direction = new Vector2();
    public GameObject m_InB;
    public GameObject m_OutB;
    public GameObject m_ArrowUp;
    public GameObject m_ArrowDown;
    public GameObject m_ArrowLeft;
    public GameObject m_ArrowRight;
    public CallbackOprate OnDirectionChangedCallback;
    public CallbackFloat OnDistanceCallback;
    public CallbackInt OnPressCallback;
    public Callback OnDistanceMaxCallback;

    bool m_IsDown = false;
    public Align m_Align = Align.ALIGN_LEFT;
    public enum Align
    {
        ALIGN_LEFT,
        ALIGN_RIGHT,
    }

    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
        int findID = -1;
        for(int i=0; i<Input.touchCount; ++i)
        {
            var touch = Input.GetTouch(i);
            if(touch.position.x > Screen.width / 2 && m_Align == Align.ALIGN_RIGHT)
            {
                findID = i;
            }
            else if (touch.position.x < Screen.width / 2 && m_Align == Align.ALIGN_LEFT)
            {
                findID = i;
            }
        }


        if(m_IsDown && findID != -1)
        {
            var touch = Input.GetTouch(findID);
            var diffent = touch.position - new Vector2(m_OutB.transform.position.x, m_OutB.transform.position.y);
            //var diffent = Input.mousePosition - m_OutB.transform.position;
            Direction = diffent.normalized;
            float maxRadius = GetInBSize().x /2;

            float currentDistance = diffent.magnitude;
            if (OnDistanceCallback != null)
                OnDistanceCallback(currentDistance);
            if (diffent.magnitude < maxRadius / 3)
            {
                if(OnPressCallback != null)
                    OnPressCallback(1);
                return;
            }
            else
            {
                if (diffent.magnitude > maxRadius)
                {
                    diffent = Direction * maxRadius;
                    if(OnDistanceMaxCallback != null)
                    {
                        OnDistanceMaxCallback();
                    }
                }

                m_InB.GetComponent<RectTransform>().localPosition = diffent;

                if (OnDirectionChangedCallback != null)
                {
                    m_ArrowUp.SetActive(false);
                    m_ArrowDown.SetActive(false);
                    m_ArrowLeft.SetActive(false);
                    m_ArrowRight.SetActive(false);
                    switch (OnDirectionChangedCallback(Direction, 1))
                    {
                        case Control.ShipOprate.MOVE_UP:
                            m_ArrowUp.SetActive(true);
                            break;
                        case Control.ShipOprate.MOVE_DOWN:
                            m_ArrowDown.SetActive(true);
                            break;
                        case Control.ShipOprate.MOVE_LEFT:
                            m_ArrowLeft.SetActive(true);
                            break;
                        case Control.ShipOprate.MOVE_RIGHT:
                            m_ArrowRight.SetActive(true);
                            break;
                    }
                }
            }
        }
    }
    public void OnInBDown(BaseEventData eventData)
    {
        var pointEventData = eventData as PointerEventData;
        m_IsDown = true;
    }
    public void OnInBUp(BaseEventData eventData)
    {
        var pointEventData = eventData as PointerEventData;
        m_InB.transform.localPosition = new Vector3();
        m_IsDown = false;

        m_ArrowUp.SetActive(true);
        m_ArrowDown.SetActive(true);
        m_ArrowLeft.SetActive(true);
        m_ArrowRight.SetActive(true);
    }

    Vector2 GetInBSize()
    {
        return m_InB.GetComponent<RectTransform>().rect.size;
    }
}
